﻿using UnityEngine;
using System.Collections;

public class Jogger : MonoBehaviour {

	public RuntimeAnimatorController _runningSprite;
	public RuntimeAnimatorController _pantingSprite;


	public float naturalSpeed = 0;
	public float speedBoost = 0;
	public float speedBoostLoss = 0.0001f;
	public float pantSpeed = 0;
	public int pantDuration = 0;

	private float acceleration = 0;
	private int team;

	public const int YOUR_TEAM = 0;
	public const int RED_TEAM = 1;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		move ();
	}

	public int getPantDuration() {
		return pantDuration;
	}

	public void setPantDuration(int pantDuration) {
		this.pantDuration = pantDuration;
	}

	public float getNaturalSpeed() {
		return	naturalSpeed;
	}
	
	public void setNaturalSpeed(float naturalSpeed) {
		this.naturalSpeed = naturalSpeed;
	}


	public float getSpeedBoost() {
		return	speedBoost;
	}

	public void setSpeedBoost(float speed) {
		this.speedBoost = speed;
	}

	public float getSpeed() {
		return (pantDuration > 0)?  pantSpeed : getSpeedBoost() + getNaturalSpeed();
	}

	public float getAcceleration() {
		return acceleration;
	}

	public void setAcceleration(float acceleration) {
		this.acceleration = acceleration;
	}

	public float randomMove() {
		return Random.Range(0, 10) / 10;
	}

	public int getTeam() {
		return team;
	}

	public void setTeam(int team) {
		this.team = team;
	}


	public void move() {

		if (getSpeed () <= pantSpeed) {
			Animator myRenderer = gameObject.GetComponentInChildren<Animator> ();
			myRenderer.runtimeAnimatorController = _pantingSprite;
		} else {
			Animator myRenderer = gameObject.GetComponentInChildren<Animator> ();
			myRenderer.runtimeAnimatorController = _runningSprite;
		}

		this.gameObject.transform.Translate (
			(getSpeed() + randomMove()),
			0,
			0
			);
		decaySpeed();
	}

	public void decaySpeed() {
			//if ( Mathf.Abs(getSpeedBoost ()) < 0.0001) {
			if (getSpeedBoost () < 0.0001) {
						setSpeedBoost (0);
				} else {
			setSpeedBoost (getSpeedBoost () - speedBoostLoss);
				}

		// decay panting duration
		if (getPantDuration() > 0) {
			setPantDuration(getPantDuration() -1);
		}

	}



}
